﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace GXRTBTC.PublicClassLibrary.Util
{
    public static class EnumHelper
    {
        /// <summary>
        /// 枚举变量是否包含指定标识
        /// </summary>
        /// <param name="value"></param>
        /// <param name="flag"></param>
        /// <returns></returns>
        public static bool Has(this Enum value, Enum flag)
        {
            if (value.GetType() != flag.GetType())
            {
                return false;
            }

            ulong num = Convert.ToUInt64(flag);
            return (Convert.ToUInt64(value) & num) == num;
        }

        /// <summary>
        /// 获取枚举字段的注释
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetDescription(this Enum value)
        {
            if (value == null)
            {
                return null;
            }

            FieldInfo field = value.GetType().GetField(value.ToString(), BindingFlags.Static | BindingFlags.Public);
            if (field == null)
            {
                return null;
            }

            DescriptionAttribute customAttribute = field.GetCustomAttribute<DescriptionAttribute>(inherit: false);
            if (customAttribute != null && !string.IsNullOrEmpty(customAttribute.Description))
            {
                return customAttribute.Description;
            }

            return null;
        }

        /// <summary>
        /// 获取枚举类型的所有字段注释
        /// </summary>
        /// <typeparam name="TEnum"></typeparam>
        /// <returns></returns>
        public static Dictionary<TEnum, string> GetDescriptions<TEnum>()
        {
            Dictionary<TEnum, string> dictionary = new Dictionary<TEnum, string>();
            foreach (KeyValuePair<int, string> description in GetDescriptions(typeof(TEnum)))
            {
                dictionary.Add((TEnum)Enum.ToObject(typeof(TEnum), description.Key), description.Value);
            }

            return dictionary;
        }

        /// <summary>
        /// 获取枚举类型的所有字段注释
        /// </summary>
        /// <param name="enumType"></param>
        /// <returns></returns>
        public static Dictionary<int, string> GetDescriptions(Type enumType)
        {
            Dictionary<int, string> dictionary = new Dictionary<int, string>();
            FieldInfo[] fields = enumType.GetFields(BindingFlags.Static | BindingFlags.Public);
            foreach (FieldInfo fieldInfo in fields)
            {
                if (fieldInfo.IsStatic)
                {
                    int key = Convert.ToInt32(fieldInfo.GetValue(null));
                    string value = fieldInfo.Name;
                    DisplayNameAttribute customAttribute = fieldInfo.GetCustomAttribute<DisplayNameAttribute>(inherit: false);
                    if (customAttribute != null && !string.IsNullOrEmpty(customAttribute.DisplayName))
                    {
                        value = customAttribute.DisplayName;
                    }

                    DescriptionAttribute customAttribute2 = fieldInfo.GetCustomAttribute<DescriptionAttribute>(inherit: false);
                    if (customAttribute2 != null && !string.IsNullOrEmpty(customAttribute2.Description))
                    {
                        value = customAttribute2.Description;
                    }

                    dictionary[key] = value;
                }
            }

            return dictionary;
        }

        /// <summary>
        /// 获取枚举类型的所有字段名称
        /// </summary>
        /// <param name="enumType"></param>
        /// <returns></returns>
        public static Dictionary<string, string> GetEnumNames(Type enumType)
        {
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            FieldInfo[] fields = enumType.GetFields(BindingFlags.Static | BindingFlags.Public);
            foreach (FieldInfo fieldInfo in fields)
            {
                if (fieldInfo.IsStatic)
                {
                    //int key = Convert.ToInt32(fieldInfo.GetValue(null));
                    string key = fieldInfo.Name;
                    string value = "";
                    DisplayNameAttribute customAttribute = fieldInfo.GetCustomAttribute<DisplayNameAttribute>(inherit: false);
                    if (customAttribute != null && !string.IsNullOrEmpty(customAttribute.DisplayName))
                    {
                        value = customAttribute.DisplayName;
                    }

                    DescriptionAttribute customAttribute2 = fieldInfo.GetCustomAttribute<DescriptionAttribute>(inherit: false);
                    if (customAttribute2 != null && !string.IsNullOrEmpty(customAttribute2.Description))
                    {
                        value = customAttribute2.Description;
                    }

                    dictionary[key] = value;
                }
            }

            return dictionary;
        }

        /// <summary>
        /// 获取string获取枚举值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool HasValue<T>(this string value) where T : struct
        {
            //return Enum.TryParse(value, true, out T result);
            return GetEnumNames(typeof(T)).ContainsKey(value);
        }
    }
}
